package cn.gudian.cms.customer

import cn.gudian.cms.GuestBook.GuestBook
import cn.gudian.cms.News.Article
import cn.gudian.cms.News.ArcType
import groovy.sql.Sql
import cn.gudian.cms.System.Page
import cn.gudian.cms.Job.Place
import cn.gudian.cms.utils.DateUtil
import cn.gudian.cms.Comment.Comment

class CController {
  //def dataSource;
  def index = {
    def articleInstanceList = Article.findAll("from Article where arc_type_id in(select id from ArcType where mode=?) and ismake=true   order by orderNum desc, id desc", ["Article"], [max: 2]);
    def bannerInstanceList = Article.findAll("from Article where arc_type_id in(select id from ArcType where mode=?) and ismake=true   order by orderNum desc, id desc", ["Banner"]);
    [articleInstanceList: articleInstanceList,bannerInstanceList:bannerInstanceList]
  }
  def guest = {
    params.max = Math.min(params.max ? params.max.toInteger() : 5, 100)
    [guestBookInstanceList: GuestBook.findAllByAudit(true, params), guestBookInstanceTotal: GuestBook.countByAudit(true)]
  }
  def blog = {
    def arcTypeID = params?.id
    params.max = Math.min(params.max ? params.max.toInteger() : 5, 100)
    params.offset = Math.min(params.offset ? params.offset.toInteger() : 0, 100)
    def articleInstanceList = null;
    def articleInstanceTotal = null;

    if (arcTypeID == null || arcTypeID == "") {

      articleInstanceList = Article.findAll("from Article  where arc_type_id in(select id from ArcType where mode=?) order by id desc", ["Article"], [max: params.max, offset: params.offset]);
      articleInstanceTotal = Article.executeQuery("select count(*) from Article where arc_type_id in(select id from ArcType where mode=?)", ["Article"]);
    } else {
      articleInstanceList = Article.findAll("from Article where arc_type_id=? order by id desc", [arcTypeID], [max: params.max, offset: params.offset]);
      articleInstanceTotal = Article.executeQuery("select count(*) from Article where arc_type_id=?", [arcTypeID]);
    }
    [articleInstanceList: articleInstanceList, articleInstanceTotal: articleInstanceTotal[0]]

  }
  def blogShow = {
    def articleID = params?.id
    def articleInstance = null
    def commentInstanceList = null;
    def commentInstanceTotal = null;
    if (articleID) {
      articleInstance = Article.find("from Article where id=? and ismake=true", [Long.parseLong(articleID)]);
      params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
      params.offset = Math.min(params.offset ? params.offset.toInteger() : 0, 100)
      commentInstanceList = Comment.findAll("from Comment where mode=? and modeID=? and audit=true order by id desc", ["Article", Integer.parseInt(articleID)], [max: params.max, offset: params.offset])
      commentInstanceTotal = Comment.executeQuery("select count(*) from Comment where mode=? and modeID=? and audit=true", ["Article", Integer.parseInt(articleID)]);

    }


    if (articleInstance)
      [articleInstance: articleInstance, commentInstanceList: commentInstanceList, commentInstanceTotal: commentInstanceTotal]
    else {
      render "内容不存在或者没有通过审核"
    }

  }
  def order = {

  }

  def product = {
    def arcTypeID = params?.id
    params.max = Math.min(params.max ? params.max.toInteger() : 4, 100)
    params.offset = Math.min(params.offset ? params.offset.toInteger() : 0, 100)
    def articleInstanceList = null;
    def articleInstanceTotal = null;
    if (arcTypeID == null || arcTypeID == "") {
      articleInstanceList = Article.findAll("from Article where arc_type_id in(select id from ArcType where mode=?) order by id desc", ["Product"], [max: params.max, offset: params.offset]);
      articleInstanceTotal = Article.executeQuery("select count(*) from Article where arc_type_id in(select id from ArcType where mode=?)", ["Product"]);
    } else {
      articleInstanceList = Article.findAll("from Article where arc_type_id=? order by id desc", [arcTypeID], [max: params.max, offset: params.offset]);
      articleInstanceTotal = Article.executeQuery("select count(*) from Article where arc_type_id=?", [arcTypeID]);
    }
    [articleInstanceList: articleInstanceList, articleInstanceTotal: articleInstanceTotal[0]]
  }

  def productShow = {
    def articleID = params?.id
    def articleInstance = null
    if (articleID)
      articleInstance = Article.find("from Article where id=? and ismake=true", [Long.parseLong(articleID)]);

    if (articleInstance)
      [articleInstance: articleInstance]
    else {
      render "内容不存在或者没有通过审核"
    }

  }

  def job = {
    def placeInstanceList = Place.list();
    [placeInstanceList: placeInstanceList]
  }
  def showCase = {

    response.sendRedirect("/case.html")
    return

  }
  def showblog = {

    redirect(url: "/blog.html")
  }

  def gudNews = {
    def articleInstanceList = Article.findAll("from Article where arc_type_id in(select id from ArcType where mode=?) and ismake=true   order by orderNum desc, id desc", ["Article"], [max: 2]);
    articleInstanceList.each {
      render """
        document.writeln("<a href='http://www.gudkj.com/c/blogShow/${it?.id}' target='_blank'>${it?.title}&nbsp;&nbsp;[${DateUtil.dateToString(it?.pubdate, "yyyy-MM-dd")}]</a>")
          """
    }

  }

  def backGuest = {

  }

  def about={
    
  }
  def service={
    
  }
}
